package com.dkm.ec.home;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class HomeOrderUtil {
	public static String buildOrderNo(String lastOrderNo,String machineId){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date=new Date();
		String now=sdf.format(date);
		String orderNo=null;
		final String prefix = "YY"; 
		final String firstNo = "0001"; 
			if(StringUtils.isEmpty(lastOrderNo)){
				orderNo=prefix+now+machineId+firstNo;
			}else{
				if(lastOrderNo.indexOf(now)==-1){
					orderNo=prefix+now+machineId+firstNo;
				}else{
					String before=lastOrderNo.substring(0, lastOrderNo.length()-4);
					String end=lastOrderNo.substring(lastOrderNo.length()-4, lastOrderNo.length());
					while(end.indexOf("0")==0){
						end=end.replace("0", "");
					}
					int i=Integer.valueOf(end);
					i++;
					end=i+"";
					while(end.length()<4){
						end="0"+end;
					}
					orderNo=before+end;
					
				}
			}
		return orderNo;
	}
}
